package ControlLayer;

import java.util.ArrayList;
import java.util.Date;

import DBLayer.DBConnection;

import DBLayer.DBInvoice;


import DBLayer.IFDBInvoice;

import ModelLayer.Invoice;

public class InvoiceCtr {
	private CustomerCtr customerCtr;

	public InvoiceCtr() {
		customerCtr = new CustomerCtr();
	}

	public Invoice getInvoice(int InvoiceId) {
		IFDBInvoice dbInvoice = new DBInvoice();
		return dbInvoice.getInvoice(InvoiceId, true);
	}

	public void insertInvoice(int customerId, Date payDate, double totalPrice,
			boolean isDelivery, boolean isCashOnDelivery) {
		Invoice invoice = new Invoice();
		invoice.setCustomer(customerCtr.getCustomer(customerId));
		invoice.setPayDate(payDate);
		invoice.setTotalPrice(totalPrice);
		invoice.setDelivery(isDelivery);
		invoice.setCashOnDelivery(isCashOnDelivery);
		try {
			DBConnection.startTransaction();
			DBInvoice dbInvoice = new DBInvoice();
			dbInvoice.insertInvoice(invoice);
			DBConnection.commitTransaction();
		} catch (Exception e) {
			DBConnection.rollbackTransaction();
		}
	}

	public ArrayList<Invoice> getAllInvoice() {
		IFDBInvoice dbInvoice = new DBInvoice();
		ArrayList<Invoice> invoices = new ArrayList<Invoice>();
		invoices = dbInvoice.getAllInvoice(false);
		return invoices;
	}

	public int updateInvoice(int invoiceNo, int customerId, Date payDate,
			double totalPrice, boolean isDelivery, boolean isCashOnDelivery) {
		IFDBInvoice dbInvoice = new DBInvoice();
		Invoice invoice = new Invoice();
		invoice.setInvoiceNo(invoiceNo);
		invoice.setPayDate(payDate);
		invoice.setTotalPrice(totalPrice);
		invoice.setDelivery(isDelivery);
		invoice.setCashOnDelivery(isCashOnDelivery);
		invoice.setCustomer(customerCtr.getCustomer(customerId));
		return dbInvoice.updateInvoice(invoice);
	}

	public double getInvoicePrice(int invoiceNo) {
		return getInvoice(invoiceNo).getTotalPrice();
	}
	
	public void deleteInvoice(int invoiceNo) {
		IFDBInvoice dbInvoice = new DBInvoice();
		dbInvoice.deleteInvoice(invoiceNo);
	}
}
